package week13.week_13_client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

import week13.week_13_server.MyUtils;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class MyMainGUI extends Application {

	TextField tf_msg, tf_addr;
	TextArea ta_info;
	DatagramSocket socket = null;
	BufferedReader in = null;
	BufferedWriter out = null;

	public MyMainGUI() throws Exception{
		if(connect())
			start(new Stage());
		else {
			MyUtils.showAlert("连接服务器出错！");
		}
	}
	@Override
	public void start(Stage stage) throws Exception {
		// TODO Auto-generated method stub

		GridPane pane_com = new GridPane();

		//GridPane.setHalignment(lb_addr,HPos.RIGHT);

		Label lb_msg = new Label("Message:");
		tf_msg = new TextField();
		tf_msg.setPrefColumnCount(20);
		Button btn_send = new Button("   Send  ");
		btn_send.setOnAction(this::btnSendHandler);

		pane_com.addRow(1, lb_msg, tf_msg, btn_send);
		GridPane.setHalignment(lb_msg,HPos.RIGHT);

		pane_com.setAlignment(Pos.CENTER);
		pane_com.setHgap(20);
		pane_com.setVgap(10);
		pane_com.setMinSize(200, 60);

		TitledPane pane_01 = new TitledPane("Communication Border",pane_com);
		pane_01.setCollapsible(false);

		ta_info = new TextArea();
		//ta_info.setMinSize(500, 50);
		ScrollPane pane_info = new ScrollPane(ta_info);
		pane_info.setFitToWidth(true);
		TitledPane pane_02 = new TitledPane("Information Border",pane_info);
		pane_02.setCollapsible(false);

		VBox box = new VBox(pane_01,pane_02);
		box.setSpacing(10);
		box.setAlignment(Pos.CENTER);

		Scene scene = new Scene(box);
		stage.setScene(scene);
		stage.setTitle("简单的聊天程序，来自1707");
		stage.show();
	}

	public boolean connect (){
		try {
			socket = new DatagramSocket();
			SocketAddress endpoint = new InetSocketAddress(ClientInfo.serverAddr,ClientInfo.serverDataPort);
			socket.connect(endpoint);
			return true;
		} catch (IOException ex) {
			System.out.println("异常信息："+ex.getMessage());

			return false;
		}
	}
	public void btnDisconnectHandler(ActionEvent event){
		if (socket != null)   socket.close();
	}
	public void btnSendHandler(ActionEvent event){
		String msg_sent = tf_msg.getText();

		try {
			DatagramPacket pout  = new DatagramPacket(msg_sent.getBytes(),msg_sent.getBytes().length);
			socket.send(pout);

			System.out.println(MyUtils.getCurrentTimeString() + ",向服务器发送字符串:"+msg_sent);
			ta_info.appendText(MyUtils.getCurrentTimeString() + ",发送信息给服务器："+msg_sent+"\n");

			DatagramPacket pin = new DatagramPacket(new byte[512],512);
			socket.receive(pin);

			//socket收到的数据的长度不一定是pin包定义的数组的大小的，可以通过pin.getLength()取得接收到的数据的长度。
			//将接收到的数据转换成字符串时，可以设定从包里取多少数据，如下：
			String msg_recv=new String(pin.getData(),0, pin.getLength());

			System.out.println(MyUtils.getCurrentTimeString() + ",从服务器接收回送字符串:"+msg_recv);
			ta_info.appendText(MyUtils.getCurrentTimeString() + ",从服务器接收信息："+msg_recv+"\n");

		} catch (IOException ex) {
			System.out.println("异常信息："+ex.getMessage());
			MyUtils.showAlert("连接服务器或发送信息出错！");
		}

	}
}
